package com.hp.consumer.rabbit;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * rabbitMQ 消费者2
 *
 * @author <a href="mailto:xing.dong@sha.ontime-express.com">Xavier Dong</a>
 * @date
 */
@Component
@RabbitListener(queues = "directQueueTx")
public class RabbitConsumer2 {

    @RabbitHandler
    public void process(String msg, Channel channel, Message message) throws IOException {
        //拿到消息延迟消费
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            if (!isNull(msg)) {
                String numstr = msg.substring(3);
                Integer num = Integer.parseInt(numstr);
                if (num >= 3) {
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                            false, true);
                    //channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
                    System.out.println("get msg2 basicNack msg = " + msg);
                } else {
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    System.out.println("get msg2 basicAck msg = " + msg);
                }
            }
        } catch (Exception e) {
            //消费者处理出了问题，需要告诉队列信息消费失败
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),
                    false, true);
            System.err.println("get msg2 failed msg = " + msg);
        }
    }

    public static boolean isNull(Object obj) {
        return obj == null || obj == "" || obj == "null";
    }
}